package internal

import (
	"admin100/app/model"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"database/sql"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/frame/gmvc"
)

type MyadminRoleDao struct {
	gmvc.M
	DB      gdb.DB
	Table   string
	Columns MyadminRoleColumns
}

type MyadminRoleColumns struct {
	Id   string
	Name string
}

var MyadminRole = MyadminRoleDao{
	M:     g.DB("default").Model("myadmin_role").Safe(),
	Table: "myadmin_role",
	Columns: MyadminRoleColumns{
		Id:   "id",
		Name: "name",
	},
}

func (d *MyadminRoleDao) All() ([]*model.MyadminRole, error) {
	all, err := d.M.All()
	if err != nil {
		return nil, err
	}

	var entities []*model.MyadminRole
	if err = all.Structs(&entities); err != nil && err != sql.ErrNoRows {
		return nil, err
	}

	return entities, nil
}

func (d *MyadminRoleDao) Page(page, limit int) *MyadminRoleDao {
	return &MyadminRoleDao{M: d.M.Page(page, limit)}
}

func (d *MyadminRoleDao) HasByName(name string) bool {

	i, err := d.M.FindCount(gdb.Map{d.Columns.Name: name})
	defErr.IfPanic(err, define.DataOptFail)

	return i > 0

}

func (d *MyadminRoleDao) HasById(id int64) bool {
	i, err := d.M.FindCount(gdb.Map{d.Columns.Id: id})
	defErr.IfPanic(err, define.DataOptFail)

	return i > 0
}
